package com.tianqu.tms.www.cardect.controller;

import java.util.List;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.entity.AucCarInfo;
import com.tianqu.tms.entity.DectCodes;
import com.tianqu.tms.entity.TmsDetectInfo;
import com.tianqu.tms.entity.TmsDetectNormal;
import com.tianqu.tms.entity.TmsDetectPic;
import com.tianqu.tms.www.cardect.viewhelper.CarNormalDetectViewHelper;

/**
 * 保存普通检测报告信息Action
 * 
 * @author tianshifu
 * 
 */
public class CarWCNormalDetectTopResultController extends TmsController {

	private Logger logger = Logger
			.getLogger(CarWCNormalDetectTopResultController.class);// log
	private static final String SAVE_STATUS = "save_status";// 保存状态
	private static final String DETECT_INPUT_DATA = "detect_input_data";// 检测录入信息
	private static final String CARSERIAL = "carSerial";// 检测录入信息
	private static final String IMGLIST = "imgList";// 用于存放图片对象的list

	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();
			// 从viewhelp中取出检测信息
			// 从uc中取出车辆ID和保存状态（暂时保存和提交保存）
			String carSerial = (String) uc.getAttribute(CARSERIAL);
			uc.removeAttribute(CARSERIAL);

			String status = (String) uc.getAttribute(SAVE_STATUS);// 获取状态
																	// 存在tis_car_info表中
			uc.removeAttribute(SAVE_STATUS);

			TmsUserProfile profile = uc.getUserProfile();

			CarNormalDetectViewHelper view = (CarNormalDetectViewHelper) uc
					.getAttribute(DETECT_INPUT_DATA);// 检测信息
			// 先存检测信息 返回一个检测ID 在tis_car_info表更新和tis_detect_pic表中进行更新

			// 先根据车辆ID查询出检测ID
			// 根据车辆ID和将检测ID和车辆状态 八大配置 行驶里程 检测模式（基本）检测时间 更新者ID 事故车 车况描述
			TmsDetectInfo carInfo = TmsDetectInfo.getInstance();
			// 需要根据车辆ID查询出登记日期
			carInfo = carInfo.getWDIpadCarInfoBySerial(carSerial, 1);
			// 查询出检测ID,并更新基本信息表

			setCarInfoFromNormalViewhelp(carInfo, view);
			carInfo.setCarSerial(carSerial);// 车辆ID
			carInfo.setStatus(Integer.parseInt(status));// 车辆状态
			carInfo.setUpdateUser(profile.getUniqueId());

			// 更新基本信息表
			carInfo.updateCarInfoByNormalDetectInfo(carInfo, 1);

			// 根据检测ID更新检测信息表
			TmsDetectNormal entity = TmsDetectNormal.getInstance();
			entity = setEntityFromViewhelp(entity, view);
			entity.setId(carInfo.getCarDetectId());

			// 将entity传到entity层中转成dto传dao层更新 返回一个检测ID
			entity.updateCarNormalDetectInfoById(entity);// 更新检测报告信息

			TmsDetectPic picEntity = TmsDetectPic.getInstance();

			// 先删除该检测信息的图片信息
			picEntity.delPic(carInfo.getCarDetectId());// 第二次就是重新覆盖一次

			// 传一个list和一个检测ID，检测模式1
			List<TmsDetectPic> list = (List<TmsDetectPic>) uc.getAttribute(IMGLIST);

			String sysPath = (String) uc.getAttribute("sysPath");

			picEntity.insertTisPicInfo(list, sysPath, carInfo.getCarDetectId(),
					carInfo.getCarSerial(), 1);// 图片信息保存
			uc.removeAttribute(IMGLIST);

			// 这块需要更新下竞价基本信息表 参数：车辆ID 表显里程
			AucCarInfo auc = AucCarInfo.getInstance();

			auc.updateMeliageBySerial(carSerial, Integer.parseInt(view
					.getMileage()));

			logger
					.debug(this.getClass().getName()
							+ "#doAction was completed.");
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

	}

	/**
	 * 
	 * 车辆基本信息表更新
	 * 
	 * @throws TmsDaoException
	 */
	private TmsDetectInfo setCarInfoFromNormalViewhelp(TmsDetectInfo carInfo,
			CarNormalDetectViewHelper view) throws TmsDaoException {

		DectCodes code = DectCodes.getInstance();
		// 八大配置
		carInfo.setSkylight(Integer.parseInt(code
				.getItemCodeByItemIdAndItemValue(
						DectCodes.EIGHT_VEHICLE_CONFIGURATION, view
								.getSkylight())));
		carInfo.setLeatherSeats(Integer.parseInt(code
				.getItemCodeByItemIdAndItemValue(
						DectCodes.EIGHT_VEHICLE_CONFIGURATION, view
								.getLeatherSeats())));
		carInfo.setAluminumRim(Integer.parseInt(code
				.getItemCodeByItemIdAndItemValue(
						DectCodes.EIGHT_VEHICLE_CONFIGURATION, view
								.getAluminumRim())));
		carInfo.setGpsNavigation(Integer.parseInt(code
				.getItemCodeByItemIdAndItemValue(
						DectCodes.EIGHT_VEHICLE_CONFIGURATION, view
								.getGpsNavigation())));
		carInfo.setAutoWindow(Integer.parseInt(code
				.getItemCodeByItemIdAndItemValue(
						DectCodes.EIGHT_VEHICLE_CONFIGURATION, view
								.getAutoWindow())));
		carInfo.setAbs(Integer.parseInt(code.getItemCodeByItemIdAndItemValue(
				DectCodes.EIGHT_VEHICLE_CONFIGURATION, view.getAbs())));
		carInfo.setSrsNumber(Integer.parseInt(code
				.getItemCodeByItemIdAndItemValue(
						DectCodes.EIGHT_VEHICLE_CONFIGURATION, view
								.getSrsNumber())));
		carInfo.setFourWheelDrive(Integer.parseInt(code
				.getItemCodeByItemIdAndItemValue(
						DectCodes.EIGHT_VEHICLE_CONFIGURATION, view
								.getFourWheelDrive())));

		int disaterCar = Integer.parseInt(code.getItemCodeByItemIdAndItemValue(
				DectCodes.TRAFFIC_CAR, view.getDisasterCars()));
		carInfo.setTroubleCar(view.getTroubleCar());// 事故车 1是u 2不是
		carInfo.setCarDescribe(view.getCarDescribe());// 车况描述
		carInfo.setMileage(Integer.parseInt(view.getMileage()));// 行驶里程

		double externalGrade = view.getExternalGrade();// 外装评分
		double internalGrade = view.getInternalGrade();// 内装评分
		double engineGrade = view.getEngineGrade();// 发动机评分
		double electrinicsGrade = view.getElectrinicsGrade();// 电子设备评分
		double restsGrade = view.getRestsGrade();// 其他设备评分

		// 外装评级
		if (externalGrade >= 90) {
			carInfo.setExternalScore("A");
		}
		if (externalGrade <= 90 && externalGrade >= 70) {
			carInfo.setExternalScore("B");
		}
		if (externalGrade <= 70 && externalGrade >= 50) {
			carInfo.setExternalScore("C");
		}
		if (externalGrade <= 50 && externalGrade >= 30) {
			carInfo.setExternalScore("D");
		}
		if (externalGrade < 30) {
			carInfo.setExternalScore("E");
		}

		// 内装评级
		if (internalGrade >= 90) {
			carInfo.setInternalScore("A");
		}
		if (internalGrade <= 90 && internalGrade >= 70) {
			carInfo.setInternalScore("B");
		}
		if (internalGrade <= 70 && internalGrade >= 50) {
			carInfo.setInternalScore("C");
		}
		if (internalGrade <= 50 && internalGrade >= 30) {
			carInfo.setInternalScore("D");
		}
		if (internalGrade < 30) {
			carInfo.setInternalScore("E");
		}
		/**** 表显里程 ******/
		double zxlc = 0;
		if (Integer.parseInt(view.getMileage()) < 30000) {
			zxlc = 90;
		}
		if (Integer.parseInt(view.getMileage()) < 60000
				&& Integer.parseInt(view.getMileage()) >= 30000) {
			zxlc = 80;
		}
		if (Integer.parseInt(view.getMileage()) < 100000
				&& Integer.parseInt(view.getMileage()) >= 60000) {
			zxlc = 70;
		}
		if (Integer.parseInt(view.getMileage()) < 150000
				&& Integer.parseInt(view.getMileage()) >= 100000) {
			zxlc = 60;
		}
		if (Integer.parseInt(view.getMileage()) >= 150000) {
			zxlc = 50;
		}
		/** 使用年限 ***/
		double yearPf = 0;
		// SimpleDateFormat fm = new SimpleDateFormat("yyyy-MM-dd");
		int year = StringUtil.getCarYear(carInfo.getRegDate());
		if (year == 0) {
			yearPf = 90;
		}
		if (year < 3 && year > 0) {
			yearPf = 80;
		}
		if (year < 5 && year >= 3) {
			yearPf = 70;
		}
		if (year < 7 && year >= 5) {
			yearPf = 60;
		}
		if (year >= 7) {
			yearPf = 70;
		}
		double zf = (engineGrade * 0.3) + (externalGrade * 0.2)
				+ (yearPf * 0.15) + (internalGrade * 0.1)
				+ (electrinicsGrade * 0.1) + (restsGrade * 0.1) + (zxlc * 0.05);// 总评分
		String zfPj = "";
		if (zf >= 90) {
			zfPj = "A";
		}
		if (zf < 90 && zf >= 70) {
			zfPj = "B";
		}
		if (zf < 70 && zf >= 50) {
			zfPj = "C";
		}
		if (zf < 50 && zf >= 30) {
			zfPj = "D";
		}
		if (zf < 50 && zf >= 30) {
			zfPj = "E";
		}
		if (view.getTroubleCar() == 1) {
			// 事故车
			carInfo.setTotalScore("R");
			carInfo.setInternalScore("");
			carInfo.setExternalScore("");// 灾害车无内装外装评分

		} else if (disaterCar > 0) {
			// 灾害车
			carInfo.setTotalScore("R");
			carInfo.setInternalScore("");
			carInfo.setExternalScore("");// 灾害车无内装外装评分

		} else {
			carInfo.setTotalScore(zfPj);
		}
		return carInfo;
	}

	/**
	 * 
	 * 检测信息录入
	 * 
	 * @param entity
	 * @param view
	 * @return
	 * @throws TmsDaoException
	 */
	private TmsDetectNormal setEntityFromViewhelp(TmsDetectNormal entity,
			CarNormalDetectViewHelper view) throws TmsDaoException {
		entity.setFrontLeftChk1(transTool(view.getFrontLeftChk1()));
		entity.setFrontLeftChk2(transTool(view.getFrontLeftChk2()));
		entity.setFrontLeftChk3(transTool(view.getFrontLeftChk3()));
		entity.setFrontLeftChk4(transTool(view.getFrontLeftChk4()));
		entity.setFrontLeftChk5(transTool(view.getFrontLeftChk5()));
		entity.setFrontLeftChk6(transTool(view.getFrontLeftChk6()));
		entity.setFrontLeftChk7(transTool(view.getFrontLeftChk7()));
		entity.setFrontLeftChk8(transTool(view.getFrontLeftChk8()));
		entity.setFrontLeftChk9(transTool(view.getFrontLeftChk9()));
		entity.setInsideChk1(transTool(view.getInsideChk1()));
		entity.setInsideChk2(transTool(view.getInsideChk2()));
		entity.setInsideChk3(transTool(view.getInsideChk3()));
		entity.setInsideChk4(transTool(view.getInsideChk4()));
		entity.setInsideChk5(transTool(view.getInsideChk5()));
		entity.setInsideChk6(transTool(view.getInsideChk6()));
		entity.setInsideChk7(transTool(view.getInsideChk7()));
		entity.setInsideChk8(transTool(view.getInsideChk8()));
		entity.setInsideChk9(transTool(view.getInsideChk9()));
		entity.setInsideChk10(transTool(view.getInsideChk10()));
		entity.setInsideChk11(transTool(view.getInsideChk11()));
		entity.setInsideChk12(transTool(view.getInsideChk12()));
		entity.setInsideChk13(transTool(view.getInsideChk13()));
		entity.setInsideChk14(transTool(view.getInsideChk14()));
		entity.setInsideChk15(transTool(view.getInsideChk15()));
		entity.setInsideChk16(transTool(view.getInsideChk16()));
		entity.setInsideChk17(transTool(view.getInsideChk17()));
		entity.setInsideChk18(transTool(view.getInsideChk18()));
		entity.setInsideChk19(transTool(view.getInsideChk19()));
		entity.setInsideChk20(transTool(view.getInsideChk20()));
		entity.setInsideChk21(transTool(view.getInsideChk21()));
		entity.setInsideChk22(transTool(view.getInsideChk22()));
		entity.setInsideChk23(transTool(view.getInsideChk23()));
		entity.setInsideChk24(transTool(view.getInsideChk24()));
		entity.setInsideChk25(transTool(view.getInsideChk25()));
		entity.setInsideChk26(transTool(view.getInsideChk26()));
		entity.setInsideChk27(transTool(view.getInsideChk27()));
		entity.setInsideChk28(transTool(view.getInsideChk28()));
		entity.setInsideChk29(transTool(view.getInsideChk29()));
		entity.setInsideChk30(transTool(view.getInsideChk30()));

		entity.setPanelFenerLhChk1(transTool(view.getPanelFenerLhChk1()));
		entity.setPanelFenerLhChk2(transTool(view.getPanelFenerLhChk2()));
		entity.setPanelFenerLhChk3(transTool(view.getPanelFenerLhChk3()));
		entity.setPanelFenerLhChk4(transTool(view.getPanelFenerLhChk4()));
		entity.setPanelFenerLhChk5(transTool(view.getPanelFenerLhChk5()));
		entity.setPanelFenerLhChk6(transTool(view.getPanelFenerLhChk6()));
		/** 四、正前面检测 **/
		entity.setDeadAheadChk1(transTool(view.getDeadAheadChk1()));
		entity.setDeadAheadChk2(transTool(view.getDeadAheadChk2()));
		entity.setDeadAheadChk3(transTool(view.getDeadAheadChk3()));
		entity.setDeadAheadChk4(transTool(view.getDeadAheadChk4()));
		entity.setDeadAheadChk5(transTool(view.getDeadAheadChk5()));
		entity.setDeadAheadChk6(transTool(view.getDeadAheadChk6()));
		/** 五、发动机舱内检测 **/
		entity.setEngineChk1(transTool(view.getEngineChk1()));
		entity.setEngineChk2(transTool(view.getEngineChk2()));
		entity.setEngineChk3(transTool(view.getEngineChk3()));
		entity.setEngineChk4(transTool(view.getEngineChk4()));
		entity.setEngineChk5(transTool(view.getEngineChk5()));
		entity.setEngineChk6(transTool(view.getEngineChk6()));
		entity.setEngineChk7(transTool(view.getEngineChk7()));
		entity.setEngineChk8(transTool(view.getEngineChk8()));
		/** 六、右前翼子板检测 **/
		entity.setPanelFenerRhChk1(transTool(view.getPanelFenerRhChk1()));
		entity.setPanelFenerRhChk2(transTool(view.getPanelFenerRhChk2()));
		entity.setPanelFenerRhChk3(transTool(view.getPanelFenerRhChk3()));
		entity.setPanelFenerRhChk4(transTool(view.getPanelFenerRhChk4()));
		entity.setPanelFenerRhChk5(transTool(view.getPanelFenerRhChk5()));
		entity.setPanelFenerRhChk6(transTool(view.getPanelFenerRhChk6()));
		/** 七、右前门检测 **/
		entity.setFrontRightChk1(transTool(view.getFrontRightChk1()));
		entity.setFrontRightChk2(transTool(view.getFrontRightChk2()));
		entity.setFrontRightChk3(transTool(view.getFrontRightChk3()));
		entity.setFrontRightChk4(transTool(view.getFrontRightChk4()));
		entity.setFrontRightChk5(transTool(view.getFrontRightChk5()));
		entity.setFrontRightChk6(transTool(view.getFrontRightChk6()));
		entity.setFrontRightChk7(transTool(view.getFrontRightChk7()));
		entity.setFrontRightChk8(transTool(view.getFrontRightChk8()));
		entity.setFrontRightChk9(transTool(view.getFrontRightChk9()));
		entity.setFrontRightChk10(transTool(view.getFrontRightChk10()));
		entity.setFrontRightChk11(transTool(view.getFrontRightChk11()));
		entity.setFrontRightChk12(transTool(view.getFrontRightChk12()));
		entity.setFrontRightChk13(transTool(view.getFrontRightChk13()));
		/** 八、右后门检测 **/
		entity.setRearRightChk1(transTool(view.getRearRightChk1()));
		entity.setRearRightChk2(transTool(view.getRearRightChk2()));
		entity.setRearRightChk3(transTool(view.getRearRightChk3()));
		entity.setRearRightChk4(transTool(view.getRearRightChk4()));
		entity.setRearRightChk5(transTool(view.getRearRightChk5()));
		entity.setRearRightChk6(transTool(view.getRearRightChk6()));
		entity.setRearRightChk7(transTool(view.getRearRightChk7()));
		entity.setRearRightChk8(transTool(view.getRearRightChk8()));
		entity.setRearRightChk9(transTool(view.getRearRightChk9()));
		entity.setRearRightChk10(transTool(view.getRearRightChk10()));
		entity.setRearRightChk11(transTool(view.getRearRightChk11()));
		/** 九、右后翼子板检测 **/
		entity.setFenderRaChk1(transTool(view.getFenderRaChk1()));
		entity.setFenderRaChk2(transTool(view.getFenderRaChk2()));
		entity.setFenderRaChk3(transTool(view.getFenderRaChk3()));
		entity.setFenderRaChk4(transTool(view.getFenderRaChk4()));
		entity.setFenderRaChk5(transTool(view.getFenderRaChk5()));
		entity.setFenderRaChk6(transTool(view.getFenderRaChk6()));
		/** 十、正后面检测 **/
		entity.setRightAsternChk1(transTool(view.getRightAsternChk1()));
		entity.setRightAsternChk2(transTool(view.getRightAsternChk2()));
		entity.setRightAsternChk3(transTool(view.getRightAsternChk3()));
		entity.setRightAsternChk4(transTool(view.getRightAsternChk4()));
		entity.setRightAsternChk5(transTool(view.getRightAsternChk5()));
		entity.setRightAsternChk6(transTool(view.getRightAsternChk6()));
		/** 十一、行李箱内侧检测 **/
		entity.setLuggageChk1(transTool(view.getLuggageChk1()));
		entity.setLuggageChk2(transTool(view.getLuggageChk2()));
		entity.setLuggageChk3(transTool(view.getLuggageChk3()));
		entity.setLuggageChk4(transTool(view.getLuggageChk4()));
		entity.setLuggageChk5(transTool(view.getLuggageChk5()));
		entity.setLuggageChk6(transTool(view.getLuggageChk6()));
		entity.setLuggageChk7(transTool(view.getLuggageChk7()));
		entity.setLuggageChk8(transTool(view.getLuggageChk8()));
		entity.setLuggageChk9(transTool(view.getLuggageChk9()));
		entity.setLuggageChk10(transTool(view.getLuggageChk10()));
		/** 十二、左后翼子板检测 **/
		entity.setFenderLaChk1(transTool(view.getFenderLaChk1()));
		entity.setFenderLaChk2(transTool(view.getFenderLaChk2()));
		entity.setFenderLaChk3(transTool(view.getFenderLaChk3()));
		entity.setFenderLaChk4(transTool(view.getFenderLaChk4()));
		entity.setFenderLaChk5(transTool(view.getFenderLaChk5()));
		entity.setFenderLaChk6(transTool(view.getFenderLaChk6()));
		/** 十三、左后门检测 **/
		entity.setRearLeftChk1(transTool(view.getRearLeftChk1()));
		entity.setRearLeftChk2(transTool(view.getRearLeftChk2()));
		entity.setRearLeftChk3(transTool(view.getRearLeftChk3()));
		entity.setRearLeftChk4(transTool(view.getRearLeftChk4()));
		entity.setRearLeftChk5(transTool(view.getRearLeftChk5()));
		entity.setRearLeftChk6(transTool(view.getRearLeftChk6()));
		entity.setRearLeftChk7(transTool(view.getRearLeftChk7()));
		entity.setRearLeftChk8(transTool(view.getRearLeftChk8()));
		entity.setRearLeftChk9(transTool(view.getRearLeftChk9()));
		entity.setRearLeftChk10(transTool(view.getRearLeftChk10()));
		entity.setRearLeftChk11(transTool(view.getRearLeftChk11()));
		/** 十四、车顶位置 **/
		entity.setCarRoofChk1(transTool(view.getCarRoofChk1()));
		/******************************/
		entity.setExternalGrade(view.getExternalGrade());// 外装评分
		entity.setInternalGrade(view.getInternalGrade());// 内装评分
		entity.setEngineGrade(view.getEngineGrade());// 发动机评分
		entity.setElectrinicsGrade(view.getElectrinicsGrade());// 电子设备评分
		entity.setRestsGrade(view.getRestsGrade());// 其他设备评分

		DectCodes code = DectCodes.getInstance();
		entity.setDisasterCars(code.getItemCodeByItemIdAndItemValue(
				DectCodes.TRAFFIC_CAR, view.getDisasterCars()));// 事故车

		return entity;
	}

	/**
	 * 将“-”转换成“”
	 * 
	 * @param str
	 * @return
	 */
	private String transTool(String str) {
		if ("-".equals(str)) {
			return "";
		}
		return str;
	}
}
